package cn.mw.cmdb.link.strategy.manager;

import cn.mw.cmdb.link.entity.LinkHistoryTrendDto;
import cn.mw.cmdb.link.entity.MwLinkTargetIpInfo;
import cn.mw.cmdb.link.entity.MwNetWorkLinkDto;
import cn.mw.cmdb.link.enums.LinkDetectionWayEnum;
import cn.mw.cmdb.link.param.LinkParam;
import cn.mw.cmdb.link.strategy.LinkDetectionWayHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description 线路检测方式策略管理
 */
@Component
@Slf4j
public class LinkDetectionWayManage {

    Map<LinkDetectionWayEnum, LinkDetectionWayHandler> handlerMap = new HashMap<>();

    @Autowired
    private ApplicationContext applicationContext;

    @PostConstruct
    public void init(){
        Map<String, LinkDetectionWayHandler> beans = applicationContext.getBeansOfType(LinkDetectionWayHandler.class);
        for (LinkDetectionWayHandler bean : beans.values()) {
            LinkDetectionWayEnum wayEnum = bean.handlerType();
            handlerMap.put(wayEnum,bean);
        }
    }

    /**
     * 获取线路的检测数据
     */
    public void getLinkDetectionData(LinkDetectionWayEnum detectionWayEnum,List<MwNetWorkLinkDto> dtos) throws Exception {
        if(detectionWayEnum == null){return;}
        handlerMap.get(detectionWayEnum).getLinkMonitorInfo(dtos);
    }

    public List<MwLinkTargetIpInfo> getIpInfo(LinkDetectionWayEnum detectionWayEnum, LinkParam linkParam) throws Exception {
        if(detectionWayEnum == null){return new ArrayList<>();}
        return handlerMap.get(detectionWayEnum).getTargetIpInfo(linkParam);
    }

    public void handlerLinkData(LinkDetectionWayEnum detectionWayEnum,MwNetWorkLinkDto netWorkLinkDto) throws Exception {
        if(detectionWayEnum == null){return;}
        handlerMap.get(detectionWayEnum).handlerLinkData(netWorkLinkDto);
    }

    public List<LinkHistoryTrendDto> getLinkHistoryTrendData(LinkDetectionWayEnum detectionWayEnum, MwNetWorkLinkDto netWorkLinkDto, LinkParam linkParam) throws Exception {
        if(detectionWayEnum == null){return null;}
        return handlerMap.get(detectionWayEnum).getLinkHistoryTrend(netWorkLinkDto,linkParam);
    }
}
